package com.abigdreamer.im.server.ui.resourse;

import java.awt.Image;
import java.awt.Toolkit;
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.filechooser.FileSystemView;

import sun.awt.shell.ShellFolder;

/**
 * 
 * 界面资源管理类
 */
public class ResourseMannager {

	/**
	 * 通过图标的名字获得图标对象，
	 * 
	 * 但这个图标必须得在ResourseMannager的同级目录下
	 * 
	 * @param iconName
	 *            :窗口图标的名字
	 * @return：还回窗口图标对象
	 */
	public static ImageIcon getIconUrlByName(String iconName) {
		java.net.URL iconUrl = ResourseMannager.class.getResource(iconName);
		javax.swing.ImageIcon icon = new javax.swing.ImageIcon(iconUrl);
		return icon;
	}

	/**
	 * 
	 * @param imagename
	 * @return
	 */
	public static Icon getIcon(String imagename) {
		java.net.URL url = ResourseMannager.class.getResource(imagename);
		Icon icon = new ImageIcon(url);
		return icon;
	}

	/**
	 * 
	 * @param ext
	 * @return
	 */
	public static Icon getIconByExtractor(String ext, boolean isBig) {
		File file = null;
		Icon icon = null;
		try {
			file = File.createTempFile("icon", "." + ext);
			FileSystemView view = FileSystemView.getFileSystemView();
			if (isBig) {
				ShellFolder shellFolder = ShellFolder.getShellFolder(file);
				icon = new ImageIcon(shellFolder.getIcon(true));
			} else {
				icon = view.getSystemIcon(file);
			}
			file.delete();
		} catch (IOException e) {
			e.printStackTrace();
			icon = getIcon("computer.png");
		}

		return icon;
	}

	public static Image getImage(String imagename) {
		java.net.URL url = ResourseMannager.class.getResource(imagename);
		Image icon = Toolkit.getDefaultToolkit().createImage(url);
		return icon;
	}

	/**
	 * 
	 * @param imagePath
	 *            相对于ResourcesManagement的路径
	 * @return
	 */
	public static Image getImage(String imagePath, int x, int y) {
		java.net.URL url = ResourseMannager.class.getResource(imagePath);
		Image image = Toolkit.getDefaultToolkit().getImage(url);
		image = image.getScaledInstance(x, y, Image.SCALE_SMOOTH);
		return image;
	}

	/**
	 * 
	 * @param imagePath
	 * @return
	 */
	public static ImageIcon getImageIcon(String imagePath, int x, int y) {
		ImageIcon imageIcon = getIconUrlByName(imagePath);
		int width = imageIcon.getIconWidth();
		int height = imageIcon.getIconHeight();
		float xbili = (float) ((x * 1.0) / width);
		float ybili = (float) ((y * 1.0) / height);
		float bili = xbili > ybili ? ybili : xbili;
		int newwidth = (int) (width * bili);
		int newheight = (int) (height * bili);
		Image image = getImage(imagePath, newwidth, newheight);
		imageIcon.setImage(image);
		return imageIcon;
	}

	public static String[] getWallImageNames() {
		String[] strs = null;
		String path = ResourseMannager.class.getPackage().getName().replace(".", "/");
		File file = new File("src/" + path + "/wallimage");
		FileListFilter filter = new FileListFilter();
		if (file.isDirectory()) {
			File[] files = file.listFiles(filter);
			strs = new String[files.length];
			for (int i = 0; i < files.length; i++) {
				strs[i] = files[i].getName();
			}
			return strs;
		}
		strs = new String[0];
		return strs;
	}
}

class FileListFilter implements FilenameFilter {
	private String extension = "jpg";

	public boolean accept(File dir, String name) {
		boolean fileOK = true;
		if (extension != null) {
			fileOK = name.endsWith('.' + extension);
		}
		return fileOK;
	}
}
